package com.adderis.soft.domain.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.adderis.soft.domain.dao.IUsersDao;
import com.adderis.soft.domain.model.Users;

@Repository("usersDao")
public class UsersDaoImpl implements IUsersDao {

	private static final Log log = LogFactory.getLog(UsersDaoImpl.class);

	Calendar calendar = Calendar.getInstance();
	java.util.Date now = calendar.getTime();
	Timestamp nowtimesstamp = new java.sql.Timestamp(now.getTime());;

	@PersistenceContext
	private EntityManager entityManager;

	/**
	 * {@inheritDoc}
	 */
	public void persist(Users transientUsers) {
		log.debug("Persisting NomCommerial instance");
		try {
			transientUsers.setCreatetimestamp(nowtimesstamp);
			transientUsers.setStatutcode("A");
			entityManager.persist(transientUsers);

			log.debug("persist successful");

		} catch (RuntimeException re) {
			log.error("persist failed", re);
		}

	}

	public void remove(Users persistentUsers) {
		log.debug("removing Users instance");
		try {
			persistentUsers.setStatutcode("D");
			persistentUsers.setDeletiontimesstamp(nowtimesstamp);
			entityManager.merge(persistentUsers);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.debug("remove failed", re);
		}

	}

	public void remove(Integer UsersId) {
		this.remove(this.findById(UsersId));

	}

	public Users merge(Users detachedUsers) {
		log.debug("merging Users instance");
		try {
			detachedUsers.setUpdatetimesstamp(nowtimesstamp);
			Users result = entityManager.merge(detachedUsers);
			entityManager.merge(detachedUsers);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			return null;
		}
	}

	public Users findById(Integer id) {
		log.debug("getting Users instance with id: " + id);
		try {
			Users instance = entityManager.find(Users.class, id);
			log.debug("findById successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("findById failed", re);
			return null;
		}
	}

	/**
	 * {@inheritDoc}
	 */

	@SuppressWarnings("unchecked")
	public List<Users> findAll() {
		log.debug("getting all Users instance");
		try {
			Query query = entityManager.createQuery("SELECT c FROM Users c");
			List<Users> usersList = (List<Users>) query.getResultList();
			log.debug("findAll successful");
			return usersList;

		} catch (RuntimeException re) {
			log.error("findAll failed", re);
			return new ArrayList<Users>();
		}

	}

	@SuppressWarnings("unchecked")
	public List<Users> search(String searchString) {
		log.debug("Search Users instances with search string: " + searchString);
		try {
			Query query = entityManager
					.createQuery("select nc from Users nc where nc.login like :searchString or nc.nom like :searchString or nc.prenom like :searchString");
			query.setParameter("searchString", searchString);
			List<Users> usersList = (List<Users>) query.getResultList();
			log.debug("search successful");
			return usersList;
		} catch (RuntimeException re) {
			log.error("search failed", re);
			return new ArrayList<Users>();
		}
	}
	
	@SuppressWarnings("rawtypes")
	public Users login(String login, String password) {
		log.debug("getting Users instance with login : " + login);
		try {
			Users users = new Users();
			Query query = entityManager
					.createQuery("select nc from Users nc where nc.login = :login and nc.password = :password");
			query.setParameter("login", login);
			query.setParameter("password", password);
			List results = query.getResultList();
			if (!results.isEmpty())
				 users = (Users) results.get(0);
		
//			Users users =  (Users) query.	();
			
			log.debug("findBylogin & password successful");
			return users;
		} catch (RuntimeException re) {
			log.error("login failed", re);
			return null;
		}
	}

}
